/**
 * Created by zhangjinrui on 17/6/27.
 */

public class Solution33 {

    public static void main(String args[]) {
        Solution33 s = new Solution33();
        System.out.println(s.search(new int[]{3,1}, 1));
    }

    public int search(int[] nums, int target) {
        if(nums.length == 0){
            return -1;
        }
        int l = 0;
        int r = nums.length - 1;
        while (l < r) {
            int mid = (l + r) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] >= nums[l]) {
                if (nums[l] <= target && target < nums[mid]) {
                    r = mid;
                } else {
                    l = mid + 1;
                }
            } else {
                if (nums[mid] < target && target <= nums[r]) {
                    l = mid + 1;
                } else {
                    r = mid;
                }
            }
        }
        return nums[l] == target ? l : -1;

    }

}
